﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class main_uc_main_sitemap : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (!Page.IsPostBack)
        {
            LoadPage();
            LoadSiteMap();
        }
    }

    private void LoadSiteMap()
    {
        TreeNode tn = new TreeNode();
        tn.NavigateUrl = "~/";
        tn.Text = DAO.TextBaseFactory.loadText("home", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/Application.aspx";
        tn.Text = DAO.TextBaseFactory.loadText("daily_transport_exchange", this.LangId);

        TreeNode sub_tn = new TreeNode();
        sub_tn.NavigateUrl = "javascript:CreateApplication()";
        sub_tn.Text = DAO.TextBaseFactory.loadText("create_app", this.LangId);
        tn.ChildNodes.Add(sub_tn);

        sub_tn = new TreeNode();
        sub_tn.NavigateUrl = "";
        sub_tn.Text = DAO.TextBaseFactory.loadText("fav_offers", this.LangId);
        tn.ChildNodes.Add(sub_tn);

        sub_tn = new TreeNode();
        sub_tn.NavigateUrl = "~/Application.aspx";
        sub_tn.Text = DAO.TextBaseFactory.loadText("all_offers", this.LangId);
        tn.ChildNodes.Add(sub_tn);

        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 4, " + this.LangId + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr1 = cmd.ExecuteReader();
                if (this.LangId == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr1.Read())
                    {
                        sub_tn = new TreeNode();
                        sub_tn.NavigateUrl = "~/Application.aspx?catid=" + dr1["id"].ToString();
                        sub_tn.Text = dr1["name"].ToString();
                        tn.ChildNodes.Add(sub_tn);
                    }
                }
                else
                {
                    while (dr1.Read())
                    {
                        sub_tn = new TreeNode();
                        sub_tn.NavigateUrl = "~/Application.aspx?catid=" + dr1["parent_id"].ToString();
                        sub_tn.Text = dr1["name"].ToString();
                        tn.ChildNodes.Add(sub_tn);
                    }
                }
                dr1.Close();
            }
        }
        

        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/Company.aspx";
        tn.Text = DAO.TextBaseFactory.loadText("mem_company", this.LangId);

        sub_tn = new TreeNode();
        sub_tn.NavigateUrl = "~/Favorites.aspx";
        sub_tn.Text = DAO.TextBaseFactory.loadText("fav_comp", this.LangId);
        tn.ChildNodes.Add(sub_tn);

        sub_tn = new TreeNode();
        sub_tn.NavigateUrl = "~/Company.aspx";
        sub_tn.Text = DAO.TextBaseFactory.loadText("all_comp", this.LangId);
        tn.ChildNodes.Add(sub_tn);

        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 2, " + this.LangId + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (this.LangId == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        sub_tn = new TreeNode();
                        sub_tn.NavigateUrl = "~/Company.aspx?catid=" + dr["id"].ToString();
                        sub_tn.Text = dr["name"].ToString();
                        tn.ChildNodes.Add(sub_tn);
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        sub_tn = new TreeNode();
                        sub_tn.NavigateUrl = "~/Company.aspx?catid=" + dr["parent_id"].ToString();
                        sub_tn.Text = dr["name"].ToString();
                        tn.ChildNodes.Add(sub_tn);
                    }
                }
                dr.Close();
            }
        }

        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "http://forum.transportexchange.org/";
        tn.Target = "_blank";
        tn.Text = DAO.TextBaseFactory.loadText("forum", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "#";
        tn.Text = DAO.TextBaseFactory.loadText("chat", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/members-information";
        tn.Text = DAO.TextBaseFactory.loadText("member_info", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/advertising";
        tn.Text = DAO.TextBaseFactory.loadText("advertising", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/contact-administrator";
        tn.Text = DAO.TextBaseFactory.loadText("contact_admin", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/customer-service";
        tn.Text = DAO.TextBaseFactory.loadText("cust_service", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/terms";
        tn.Text = DAO.TextBaseFactory.loadText("terms_cond", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/privacy-policy";
        tn.Text = DAO.TextBaseFactory.loadText("privacy_policy", this.LangId);
        tvSitemap.Nodes.Add(tn);

        tn = new TreeNode();
        tn.NavigateUrl = "~/copyrights";
        tn.Text = DAO.TextBaseFactory.loadText("copyright", this.LangId);
        tvSitemap.Nodes.Add(tn);

    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    private void LoadPage()
    {
        string text;
        string keywords;
        Utilities.LoadPage(this.LangId, "sitemap", out text, out keywords);
        lblText.Text = text;
    }
}
